const app = getApp()
var util = require("../../utils/utils.js");
// canvas 全局配置
var context = null; // 使用 wx.createContext 获取绘图上下文 context
var isButtonDown = false;
var arrx = [];
var arry = [];
var arrz = [];
var canvasw = 0;
var canvash = 0;
//获取系统信息
wx.getSystemInfo({
  success: function(res) {
    canvasw = res.windowWidth; //设备宽度
    canvash = 620;
  }
});
Page({
  canvasIdErrorCallback: function(e) {
    console.error(e.detail.errMsg)
  },
  canvasStart: function(event) {
    isButtonDown = true;
    arrz.push(0);
    arrx.push(event.changedTouches[0].x);
    arry.push(event.changedTouches[0].y);
    //context.moveTo(event.changedTouches[0].x, event.changedTouches[0].y);

  },
  canvasMove: function(event) {
    if (isButtonDown) {
      arrz.push(1);
      arrx.push(event.changedTouches[0].x);
      arry.push(event.changedTouches[0].y);
      // context.lineTo(event.changedTouches[0].x, event.changedTouches[0].y);
      // context.stroke();
      // context.draw()

    };

    for (var i = 0; i < arrx.length; i++) {
      if (arrz[i] == 0) {
        context.moveTo(arrx[i], arry[i])
      } else {
        context.lineTo(arrx[i], arry[i])
      };

    };
    context.clearRect(0, 0, canvasw, canvash);
    context.stroke();

    context.draw(true);
  },
  canvasEnd: function(event) {
    isButtonDown = false;
  },
  cleardraw: function() {
    //清除画布
    arrx = [];
    arry = [];
    arrz = [];
    context.clearRect(0, 0, canvasw, canvash);
    context.draw(true);
  },
  /**
   * 表单提交
   */
  formSubmit:function(e){
    let name = e.detail.value.name;
    if (arrx.length == 0) {
      wx.showModal({
        title: '提示',
        content: '签名内容不能为空！',
        showCancel: false
      });
      return false;
    };
    //提交
    var that = this
    wx.showToast({
      title: '提交中...',
      icon: "loading",
      mask:true,
    })
    wx.canvasToTempFilePath({
      canvasId: 'canvas',
      fileType: 'jpg',
      success: function (res) {
        console.log(res.tempFilePath);
        console.log(that.data.childId)
        let checkbox = that.data.checkbox? "N": "Y"
        if (!e.detail.value.age){
          wx.showToast({
            title:"请输入年龄",
            icon:"none"
          })
          return;
        }
        if (!e.detail.value.sex) {
          wx.showToast({
            title: "请输入性别",
            icon: "none"
          })
          return;
        }
        if (!e.detail.value.health) {
          wx.showToast({
            title: "请输入体重",
            icon: "none"
          })
          return;
        }
        if (!e.detail.value.height) {
          wx.showToast({
            title: "请输入身高",
            icon: "none"
          })
          return;
        }
        if (!e.detail.value.size) {
          wx.showToast({
            title: "请输入鞋子尺码",
            icon: "none"
          })
          return;
        }
        if (!e.detail.value.scholl) {
          wx.showToast({
            title: "请输入就读学校",
            icon: "none"
          })
          return;
        }
        if (!e.detail.value.year) {
          wx.showToast({
            title: "请输入年级",
            icon: "none"
          })
          return;
        }
        var data;
        if(that.data.checkbox){
          data = {
            childId: that.data.childId,
            childAge: e.detail.value.age,
            childSex: e.detail.value.sex,
            childHealth: e.detail.value.health,
            childHeight: e.detail.value.height,
            childWeight: e.detail.value.weight,
            childShoeSize: e.detail.value.size,
            childSchoolName: e.detail.value.scholl,
            childGrade: e.detail.value.year,
            isDisease: checkbox,
          }
        }else{
          data = {
            childId: that.data.childId,
            childAge: e.detail.value.age,
            childSex: e.detail.value.sex,
            childHealth: e.detail.value.health,
            childHeight: e.detail.value.height,
            childWeight: e.detail.value.weight,
            childShoeSize: e.detail.value.size,
            childSchoolName: e.detail.value.school,
            childGrade: e.detail.value.year,
            isDisease: checkbox,
            diseaseDesc: e.detail.value.haves,
          }
        }

        
        console.log(data)
        //存入服务器
        wx.uploadFile({
          url: app.httpUrl + '/user/addSignProtocol', //接口地址
          filePath: res.tempFilePath,
          name: 'file',
          header: {
            'xcx-xtoken': app.globalData.openid,
            'content-type': 'application/json' // 默认值
          },
          method: "POST",
          formData: data,
          success: function (res) {
            console.log(res)
            var obj = JSON.parse(res.data)
            console.log(obj);
            let id = obj.data.protocolNo;
            wx.navigateTo({
              url: '/pages/preview/preview?id=' + id,
            })
            wx.hideToast()
          },
          fail: function (res) {
            console.log(res);
            wx.hideToast()
          },
          complete: function (res) {

          }
        });
      }
    })
  },
  getimg: function() {
    

  },
  /**
   * 获取学生信息
   * 
   */
  getstuderId:function(){
    
  },

  /**
   * 获取姓名
   */
  name:function(e){
    let name = e.detail.value;
    this.setData({
      name:name
    })
  },

  /**
   * 获取性别
   */ 
  sex:function(e){
    let sex =e.detail.value;
    this.setData({
      sex: sex
    })
  },
  /**
   * 获取年龄
   */
  age:function(e){
    let age = e.detail.value;
    this.setData({
      age: age
    })
  },

  /**
   * 获取就读学校
   */
  scholl:function(e){
    let scholl =e.detail.value;
    this.setData({
      scholl: scholl
    })
  },

  /**
   * 获取年级
   */
  year:function(e){
    let year = e.detail.value;
    this.setData({
      year: year
    })
  },

  /**
   * 获取健康状况
   */
  health:function(e){
    let health = e.detail.value;
    this.setData({
      health: health
    })
  },

  /**
   * 获取身高
   */
  height:function(e){
    let height = e.detail.value;
    this.setData({
      height: height
    })
  },

  /**
   * 获取体重
   */
  weight:function(e){
    let weight = e.detail.value;
    this.setData({
      weight: weight
    })
  },

  /**
   * 获取鞋子尺码
   */
  size:function(e){
    let size = e.detail.value;
    this.setData({
      size: size
    })
  },
  /**
   * 是否有重大疾病
   */
  checkbox:function(){
    let checkbox  = !this.data.checkbox;
    this.setData({
      checkbox: checkbox
    })
  },
  /**
   * 有重大疾病
   * 
   */
  haves:function(e){
    let haves = e.detail.value;
    this.setData({
      haves: haves
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    checkbox:false,
    childId:"",//孩子ID
  },
  /**
   * 查询小孩
   */
  getchild:function(){
    let that = this;
    let childId = that.data.childId;
    wx.request({
      url: app.httpUrl + '/user/queryChildInfo/' + childId,
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        let detail = res.data.data;
        console.log(detail)
        if (detail.isDisease=="N"){
          this.setData({
            checkbox:true,
          })
        }
        that.setData({
          detail: detail,
          name: detail.childName,
          sex: detail.childSex =='F'? "男孩" : "女孩",
          scholl: detail.childSchoolName ,
          year: detail.childGrade ,
          health: detail.childHealth ,
          weight: detail.childWeight ,
          height: detail.childHeight ,
          haevs:detail.diseaseDesc,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let childId = options.childid;
    this.setData({
      childId: childId
    })
    this.getchild()
    // 使用 wx.createContext 获取绘图上下文 context
    context = wx.createCanvasContext('canvas');
    context.beginPath()
    context.setStrokeStyle('#000000');
    context.setLineWidth(4);
    context.setLineCap('round');
    context.setLineJoin('round');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})